package com.yf.esms.controller;

import com.yf.esms.common.ResponseCode;
import com.yf.esms.common.ResultComponent;
import com.yf.esms.model.entries.User;
import com.yf.esms.model.vo.*;
import com.yf.esms.service.DocService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.action.DocWriteResponse;
import org.elasticsearch.common.unit.TimeValue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author 99698
 */
@Api(tags = "es文档")
@RestController
@Slf4j
@RequestMapping("/doc")
public class DocController {

    @Autowired
    private DocService docService;

    @PostMapping("/insertDoc")
    @ApiOperation(value = "向索引中新增一个文档")
    public ResultComponent<DocCommonVo> insertDoc(@RequestBody InsertUserVo insertUserVo) throws IOException {
        return new ResultComponent<>(true,
                "向索引"+insertUserVo.getIndexName()+"中新增文档成功！",
                "200",
                docService.insertDoc(insertUserVo));
    }

    @PostMapping("/updateDoc")
    @ApiOperation(value = "局部更新一条文档数据")
    public ResultComponent<DocWriteResponse.Result> updateDoc(@RequestBody UpdateUserVo updateUserVo) throws IOException {
        return new ResultComponent<>(true,
                "修改索引"+updateUserVo.getIndexName()+"修改文档成功！",
                "200",
                docService.updateDoc(updateUserVo));
    }

    @PostMapping("/selectDoc")
    @ApiOperation(value = "根据文档id获取文档")
    public ResultComponent<User> selectDoc(@RequestBody SelectUserVo selectUserVo) throws IOException {
        return new ResultComponent<>(true,
                "查询索引"+selectUserVo.getIndexName()+"中的id为"+selectUserVo.getId()+"的文档成功！",
                "200",
                docService.selectDoc(selectUserVo));
    }

    @PostMapping("/deleteDoc")
    @ApiOperation(value = "删除该id的文档")
    public ResultComponent<DocWriteResponse.Result> deleteDoc(@RequestBody DeleteUserVo deleteUserVo) throws IOException {
        return new ResultComponent<>(true,
                "删除索引"+deleteUserVo.getIndexName()+"中的id为"+deleteUserVo.getId()+"的文档成功！",
                "200",
                docService.deleteDoc(deleteUserVo));
    }

    @PostMapping("/insertBatch")
    @ApiOperation(value = "批量插入数据")
    public ResultComponent<List<String>> insertBatch(@RequestBody InsertBatchUsersVo insertBatchUsersVo) throws IOException {
        return new ResultComponent<>(true,
                "向索引"+insertBatchUsersVo.getIndexName()+"中批量插入数据成功！",
                "200",
                docService.insertBatch(insertBatchUsersVo));
    }

    @PostMapping("/deleteBatch")
    @ApiOperation(value = "批量删除数据")
    public ResultComponent<Long> deleteBatch(@RequestBody DeleteBatchUsersVo deleteBatchUsersVo) throws IOException {
        return new ResultComponent<>(true,
                "向索引"+deleteBatchUsersVo.getIndexName()+"中批量插入数据成功！",
                "200",
                docService.deleteBatch(deleteBatchUsersVo));
    }

    @PostMapping("/searchAll")
    @ApiOperation(value = "高级查询-全量")
    public ResultComponent<SearchUserResultVo> search(@RequestBody SearchUserVo searchUserVo) throws IOException {
        return new ResultComponent<>(true,
                "查询索引"+searchUserVo.getIndexName()+"成功！",
                "200",
                docService.searchAll(searchUserVo));
    }

    @PostMapping("/termQuery")
    @ApiOperation(value = "高级查询-条件查询")
    public ResultComponent<SearchUserResultVo> termQuery(@RequestBody SearchUserVo searchUserVo) throws IOException {
        return new ResultComponent<>(true,
                "查询索引"+searchUserVo.getIndexName()+"成功！",
                "200",
                docService.termQuery(searchUserVo));
    }

    @PostMapping("/multiCondition")
    @ApiOperation(value = "高级查询-多条件组合查询")
    public ResultComponent<SearchUserResultVo> multiCondition(@RequestBody MultiConditionVo multiConditionVo) throws IOException {
        return new ResultComponent<>(true,
                "",
                "200",
                docService.multiCondition(multiConditionVo));
    }

    @PostMapping("/range")
    @ApiOperation(value = "高级查询-范围查询")
    public ResultComponent<SearchUserResultVo> range(@RequestBody RangeSearchVo rangeSearchVo) throws IOException {
        return new ResultComponent<>(true,
                "",
                "200",
                docService.rangeSearch(rangeSearchVo));
    }

    @PostMapping("/fuzzyQuery")
    @ApiOperation(value = "高级查询-模糊查询")
    public ResultComponent<SearchUserResultVo> fuzzyQuery(@RequestBody FuzzyQueryVo fuzzyQueryVo) throws IOException {
        return new ResultComponent<>(true,
                ResponseCode.SUCCESS.getMsg(),
                ResponseCode.SUCCESS.getCode(),
                docService.fuzzyQuery(fuzzyQueryVo));
    }

    @PostMapping("/highlight")
    @ApiOperation(value = "高级查询-关键词高亮检索")
    public ResultComponent<SearchUserResultVo> highlight(@RequestBody HighlightQueryVo highlightQueryVo) throws IOException {
        return new ResultComponent<>(true,
                ResponseCode.SUCCESS.getMsg(),
                ResponseCode.SUCCESS.getCode(),
                docService.highlight(highlightQueryVo));
    }
}
